﻿using Set_File_Rw;
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using TestFireBroadcastingHostPhone.ViewModel;

namespace TestFireBroadcastingHostPhone.RelatedInstruments
{
    public class InstrumentsControl
    {
        /// <summary>
        /// 程控交流源开启的标志
        /// </summary>
        public bool open_power = false;
        /// <summary>
        /// 程控交流源串口
        /// </summary>
        public SerialPort? ACPowerPort;
        /// <summary>
        /// 实例化程控交流源对象
        /// </summary>
        public AN97002H An97002H = new AN97002H();
        /// <summary>
        /// 实例化新程控交流源对象
        /// </summary>
        public NAN97002S nAN97002S = new NAN97002S();
        /// <summary>
        /// 示波器默认IP地址
        /// </summary>
        public string OscilloscopeIP_Address = string.Empty;
        /// <summary>
        /// 定义示波器对象
        /// </summary>
        public DS1102 dS1102 = new DS1102();
        public InstrumentsControl()
        {
        }
        /// <summary>
        /// 打开程控交流源串口
        /// </summary>
        /// <param name="err_info">错误信息</param>
        private void Open_ACPower_SerialPort(string serialPortName,out string err_info)
        {
            err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini"; 
            if (!string.IsNullOrEmpty(serialPortName))
            {
                if (SystemConfigMV.systemconfigMV.AreSerialPortsUnique())
                {
                    ACPowerPort = new SerialPort();
                    ACPowerPort.PortName = serialPortName;
                    ACPowerPort.Parity = Parity.None;
                    if (ACPowerPort.PortName != null)
                        Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "交流源串口", ACPowerPort.PortName, out err_info);
                }
                else
                {
                    err_info = "选择串口存在冲突，请检查串口参数配置!";
                    return;
                }
            }
            else
            {
                err_info = "请配置程控交流源串口!";
                return;
            }
        }
        /// <summary>
        /// 打开程控交流源
        /// </summary>
        /// <param name="powerType">程控交流源类型</param>
        /// <param name="serialPortName">程控交流源串口</param>
        /// <param name="err_info">错误信息</param>
        public void OpenACPower(string powerType,string serialPortName,out string err_info)
        {
            err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            if (!open_power)
            {
                if (ACPowerPort == null)
                {
                    Open_ACPower_SerialPort(serialPortName, out err_info);//打开串口
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        return;
                    }
                }
                Thread.Sleep(100);
                if (ACPowerPort != null)
                {
                    switch (powerType)
                    {
                        case "新程控交流源":
                            ACPowerPort.BaudRate = NAN97002S.NAN97002S_BaudRate;
                            nAN97002S.Function_OpenNAN97002S(ACPowerPort, 1, out err_info);//打开新程控交流源
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                return;
                            }
                            else
                            {
                                open_power = true;
                                Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "程控交流源配置", "新程控交流源", out err_info);
                            }
                            break;
                        case "旧程控交流源":
                            ACPowerPort.BaudRate = AN97002H.AN97002H_BaudRate;
                            err_info = An97002H.ACPower_vControlStart(1, ACPowerPort);//打开旧程控交流源
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                return;
                            }
                            else
                            {
                                open_power = true;
                                Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "程控交流源配置", "旧程控交流源", out err_info);
                            }
                            break;
                        default: err_info = "未选择交流源类型!"; break;
                    }
                }
            }
        }
        /// <summary>
        /// 关闭程控交流源
        /// </summary>
        /// <param name="powerType">程控交流源类型</param>
        /// <param name="serialPortName">程控交流源串口</param>
        /// <param name="err_info">错误信息</param>
        public void CloseACPower(string powerType,string serialPortName,out string err_info)
        {
            err_info = string.Empty;
            if (open_power)
            {
                if (ACPowerPort == null)
                {
                    Open_ACPower_SerialPort(serialPortName,out err_info);//打开串口
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        return;
                    }
                }
                Thread.Sleep(100);
                if (ACPowerPort != null)
                {
                    switch (powerType)
                    {
                        case "新程控交流源":
                            ACPowerPort.BaudRate = NAN97002S.NAN97002S_BaudRate;
                            nAN97002S.Function_CloseNAN97002S(ACPowerPort, 1, out err_info);
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                return;
                            }
                            else
                            {
                                open_power = false;
                            }
                            break;
                        case "旧程控交流源":
                            ACPowerPort.BaudRate = AN97002H.AN97002H_BaudRate;
                            err_info = An97002H.ACPower_vControlStop(1, ACPowerPort);//关闭程控交流源
                            if (!string.IsNullOrEmpty(err_info))
                            {
                                return;
                            }
                            else
                            {
                                open_power = false;
                            }
                            break;
                        default: err_info = "未选择交流源类型!"; break;
                    }
                }
            }
        }
        /// <summary>
        /// 连接示波器
        /// </summary>
        public void ConnectOscilloscope(string ipAddress, out string err_info)
        {
            err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            OscilloscopeIP_Address = ipAddress;//获取示波器的IP地址
            if (!string.IsNullOrEmpty(OscilloscopeIP_Address))
            {
                dS1102.DS_Connect(OscilloscopeIP_Address, out err_info);                           //连接示波器
                if (string.IsNullOrEmpty(err_info))
                {
                    Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "示波器IP地址", OscilloscopeIP_Address, out err_info);
                }
            }
            else
            {
                err_info = "未输入示波器地址";
            }
        }
        /// <summary>
        /// 最大过压设置
        /// </summary>
        /// <param name="powerType">程控交流源类型</param>
        /// <param name="beyond_voltage">最大过压值</param>
        /// <param name="err_info">错误信息</param>
        public void Beyond_VoltageMaxTest(string powerType,string beyond_voltage, out string err_info)
        {
            err_info = string.Empty;
            if (!string.IsNullOrEmpty(beyond_voltage) && (ACPowerPort != null))
            {
                switch (powerType)
                {
                    case "新程控交流源":
                        decimal exchange_voltage = Convert.ToDecimal(beyond_voltage) * 10;
                        nAN97002S.Function_SetVoltageAndFrequencyOnline(ACPowerPort, 1, exchange_voltage, 500, 10, out err_info);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电过压设置失败：";
                            return;
                        }
                        break;
                    case "旧程控交流源":
                        err_info = An97002H.ACPower_vSetParameters(1, Convert.ToDecimal(beyond_voltage), 50, true, ACPowerPort);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电过压设置失败：";
                            return;
                        }
                        break;
                    default: err_info = "程控交流源型号选择错误!"; break;
                }
            }
        }
        /// <summary>
        /// 最小过压设置
        /// </summary>
        /// <param name="powerType">程控交流源类型</param>
        /// <param name="restore_voltage">最小过压值</param>
        /// <param name="err_info">错误信息</param>
        public void Beyond_VoltageMinTest(string powerType,string restore_voltage,out string err_info)
        {
            err_info = string.Empty;
            if (!string.IsNullOrEmpty(restore_voltage) && (ACPowerPort != null))
            {
                switch (powerType)
                {
                    case "新程控交流源":
                        decimal exchange_voltage = Convert.ToDecimal(restore_voltage) * 10;
                        nAN97002S.Function_SetVoltageAndFrequencyOnline(ACPowerPort, 1, exchange_voltage, 500, 10, out err_info);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电恢复设置失败：";
                            return;
                        }
                        break;
                    case "旧程控交流源":
                        err_info = An97002H.ACPower_vSetParameters(1, Convert.ToDecimal(restore_voltage), 50, true, ACPowerPort);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电恢复设置失败：";
                            return;
                        }
                        break;
                    default: err_info = "程控交流源型号选择错误!"; break;
                }
            }
        }
        /// <summary>
        /// 程控交流源恢复260V设置
        /// </summary>
        /// <param name="powerType">程控交流源类型</param>
        /// <param name="err_info">错误信息</param>
        public void Restore_VoltageTest(string powerType,out string err_info)
        {
            err_info = string.Empty;
            if (ACPowerPort != null)
            {
                switch (powerType)
                {
                    case "新程控交流源":
                        nAN97002S.Function_SetVoltageAndFrequencyOnline(ACPowerPort, 1, 2600, 500, 10, out err_info);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电过压设置失败：";
                            return;
                        }
                        break;
                    case "旧程控交流源":
                        err_info = An97002H.ACPower_vSetParameters(1, 260, 50, true, ACPowerPort);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电过压设置失败：";
                            return;
                        }
                        break;
                    default: err_info = "程控交流源型号选择错误!"; break;
                }
            }
        }
        /// <summary>
        /// 恢复220V正常供电
        /// </summary>
        /// <param name="powerType">程控交流源类型</param>
        /// <param name="err_info">错误信息</param>
        public void Host220_VoltageTest(string powerType,out string err_info)
        {
            err_info = string.Empty;
            if (ACPowerPort != null)
            {
                switch (powerType)
                {
                    case "新程控交流源":
                        nAN97002S.Function_SetVoltageAndFrequencyOnline(ACPowerPort, 1, 2200, 500, 10, out err_info);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电220V设置失败：";
                            return;
                        }
                        break;
                    case "旧程控交流源":
                        err_info = An97002H.ACPower_vSetParameters(1, 220, 50, true, ACPowerPort);//50Hz，锁定
                        if (!string.IsNullOrEmpty(err_info))
                        {
                            err_info += "主电220V设置失败：";
                            return;
                        }
                        break;
                    default: err_info = "程控交流源型号选择错误!"; break;
                }
            }
        }
    }
}
